package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.FundDetailRechargeFragment;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.fragments.FundDetailWithdrawFragment;

public class FundDetailActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;

    private List<Fragment> fragments;

    private List<TextView> textViews;
    private List<View> views;
    private FundDetailRechargeFragment rechargeFragment;
    private FundDetailWithdrawFragment withdrawFragment;
    private TextView recharge;
    private View recharge_line;
    private TextView withdraw;
    private View withdraw_line;
    private FundPagerAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fund_detail);

        initFragments();
        initViews();
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        rechargeFragment = new FundDetailRechargeFragment();
        fragments.add(rechargeFragment);

        withdrawFragment = new FundDetailWithdrawFragment();
        fragments.add(withdrawFragment);
    }

    private void initViews() {
        textViews = new ArrayList<>();
        views = new ArrayList<>();

        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("资金明细");

        findViewById(R.id.recharge).setOnClickListener(this);
        recharge = ((TextView) findViewById(R.id.fund_detail_recharge));
        recharge.setTextColor(Color.parseColor("#ef9e4f"));
        textViews.add(recharge);
        recharge_line = findViewById(R.id.fund_detail_recharge_line);
        recharge_line.setSelected(true);
        views.add(recharge_line);

        findViewById(R.id.withdraw).setOnClickListener(this);
        withdraw = ((TextView) findViewById(R.id.fund_detail_withdraw));
        textViews.add(withdraw);
        withdraw_line = findViewById(R.id.fund_detail_withdraw_line);
        views.add(withdraw_line);


        viewPager = ((ViewPager) findViewById(R.id.fund_detail_viewpager));
        adapter = new FundPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for(int i = 0; i < textViews.size();i++){
            textViews.get(i).setTextColor(Color.parseColor("#949494"));
            views.get(i).setSelected(false);
        }
        textViews.get(position).setTextColor(Color.parseColor("#ec9e4f"));
        views.get(position).setSelected(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class FundPagerAdapter extends FragmentPagerAdapter{

        public FundPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                finish();
                break;
            case R.id.recharge:
                viewPager.setCurrentItem(0);
                break;
            case R.id.withdraw:
                viewPager.setCurrentItem(1);
                break;
        }
    }
}
